package com.sn.socket.utils;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 客户端任务记录
 */
public class ClientTask {

    private String deviceId = null;

    private String ipAddreass = null;

    /**
     * 频率列表，记录该客户端的每个频率收录此处
     */
    private ConcurrentHashMap<String, AtomicInteger> freqsHashMap = new ConcurrentHashMap<String, AtomicInteger>(100);
    /**
     * 消息队列，记录该客户端的任务消息队列
     */
    private LinkedBlockingDeque<String> sendMessages = new LinkedBlockingDeque<>(200);

    /**
     * @return LinkedBlockingDeque<String>  消息队列
     */
    public LinkedBlockingDeque<String> getSendMessages() {
        return sendMessages;
    }

    public void setSendMessages(LinkedBlockingDeque<String> sendMessages) {
        this.sendMessages = sendMessages;
    }

    /**
     * @return 该客户端的频率清单
     */
    public ConcurrentHashMap<String, AtomicInteger> getFreqsHashMap() {
        return freqsHashMap;
    }

    public void setFreqsHashMap(ConcurrentHashMap<String, AtomicInteger> freqsHashMap) {
        this.freqsHashMap = freqsHashMap;
    }

    /**
     * @param freq  get the freq tune and recording times
     * @return int
     */
    public int getTimes(String freq) {
        if (!freqsHashMap.containsKey(freq)) {
            freqsHashMap.put(freq, new AtomicInteger(0));
        }
        return freqsHashMap.get(freq).intValue();
    }

    /**
     * @param freq increment the freq times
     */
    public void incrementTimes(String freq) {
        if (!freqsHashMap.containsKey(freq)) {
            freqsHashMap.put(freq, new AtomicInteger(0));
        }
        freqsHashMap.get(freq).incrementAndGet();
    }

    /**
     * @param messge put message to last message
     */
    public void offer(String messge) {
        this.sendMessages.offer(messge);
    }

    /**
     * @param messge put message to first messge
     */
    public void offerFirst(String messge) {
        this.sendMessages.offerFirst(messge);
    }

    /**
     * @return get the first message
     */
    public String pollMessage() {
        return this.sendMessages.poll();
    }



    @Override
    public String toString() {
        return "ClientTask{" +
                "freqsHashMap num:" + freqsHashMap.size() +
                ", sendMessages num:" + sendMessages.size() +
                '}';
    }

    /**
     * @return return deviceid
     */
    public String getDeviceId() {
        return deviceId;
    }

    /**
     * @param deviceId set deviceId
     */
    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getIpAddreass() {
        return ipAddreass;
    }

    public void setIpAddreass(String ipAddreass) {
        this.ipAddreass = ipAddreass;
    }
}
